package com.hhx.androidrxmvp.net;

import android.content.Context;
import android.util.Log;

import com.google.gson.JsonParseException;
import com.hhx.androidrxmvp.net.exception.ApiException;
import com.hhx.androidrxmvp.net.exception.ExceptionEngine;
import com.hhx.androidrxmvp.utils.ToastUtil;

import org.json.JSONException;

import java.net.ConnectException;
import java.net.SocketTimeoutException;
import java.net.UnknownHostException;
import java.net.UnknownServiceException;
import java.text.ParseException;

import io.reactivex.Observer;
import io.reactivex.disposables.Disposable;
import retrofit2.HttpException;

/**
 * Created by Administrator on 2018/12/5.
 */

public class ProgressLoadingObserver<T> implements Observer<T>, ProgressCancelListener {

    private static final String TAG = "ProgressLoadingObserver";

    private Context mContext;
    private ObserverResponseListener<T> mListener;
    private LoadingDialogHandler mLoadingDialogHandler;
    private Disposable mDisposable;

    public ProgressLoadingObserver(Context mContext, ObserverResponseListener<T> mObserverResponseListener, boolean isDialog, boolean cancelable) {
        this.mContext = mContext;
        this.mListener = mObserverResponseListener;
        if (isDialog) {
            mLoadingDialogHandler = new LoadingDialogHandler(mContext, this, cancelable);
        }
    }

    private void showLoadingDialog() {
        if (mLoadingDialogHandler != null) {
            mLoadingDialogHandler.obtainMessage(LoadingDialogHandler.SHOW_PROGRESS_DIALOG).sendToTarget();
        }
    }

    private void dismissLoadingDialog() {
        if (mLoadingDialogHandler != null) {
            mLoadingDialogHandler.obtainMessage(LoadingDialogHandler.DISMISS_PROGRESS_DIALOG).sendToTarget();
            mLoadingDialogHandler = null;
        }
    }

    @Override
    public void onSubscribe(Disposable d) {
        this.mDisposable = d;
        showLoadingDialog();
    }

    @Override
    public void onNext(T t) {
        mListener.onNext(t);//可定制接口，通过code回调处理不同的业务
    }

    @Override
    public void onError(Throwable e) {
        dismissLoadingDialog();
        Log.e(TAG, "onError: " + e);
        //自定义异常处理
        if (e instanceof ApiException) {
            mListener.onError((ApiException) e);
            //mListener.onError((ExceptionHandle.ResponeThrowable) e);
        } else {
            mListener.onError(new ApiException(e, ExceptionEngine.UN_KNOWN_ERROR));
            //mListener.onError(new ExceptionHandle.ResponeThrowable(e, ExceptionHandle.ERROR.UNKNOWN));
        }

        if (e instanceof UnknownHostException) {
            ToastUtil.showLong("没有网络，请检查网络连接");
        } else if (e instanceof SocketTimeoutException) {
            ToastUtil.showLong("请求超时");
        } else if (e instanceof ConnectException) {
            ToastUtil.showLong("网络连接异常，请检查网络");
        } else if (e instanceof JsonParseException
                || e instanceof JSONException
                || e instanceof ParseException) {
            ToastUtil.showLong("数据解析异常");
        } else if (e instanceof HttpException) {
            ToastUtil.showLong("请求超时");
        } else if (e instanceof IllegalArgumentException) {
            ToastUtil.showLong("服务器错误");
        } else if (e instanceof UnknownServiceException) {
            ToastUtil.showLong("未知的服务器错误");
        } else {
            ToastUtil.showLong("服务器错误");
        }
    }

    @Override
    public void onComplete() {
        dismissLoadingDialog();
        Log.e(TAG, "onComplete: ");
    }

    @Override
    public void onCancelProgress() {
        Log.e(TAG, "requestCancel: ");
        //如果处于订阅状态，则取消订阅
        if (!mDisposable.isDisposed()) {
            mDisposable.dispose();
        }
    }
}
